package com.zdc.learn.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 自定义解码器
 */
public class MessageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int length = in.readInt();

        byte[] content = new byte[length];

        in.readBytes(content);

        //封装成 messageStyle 对象，放入out ,传递给下一个handler 业务处理
        MessageStyle messageStyle = new MessageStyle();
        messageStyle.setLen(length);
        messageStyle.setContent(content);
        out.add(messageStyle);


    }
}
